package com.BlogPark.service.CollectionService.Impl;

import com.BlogPark.mapper.ArticleMapper;
import com.BlogPark.mapper.CollectionsMapper;
import com.BlogPark.pojo.Article;
import com.BlogPark.pojo.Collections;
import com.BlogPark.service.CollectionService.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: MFM
 * @Date: 2022/8/10 - 08 - 10 - 21:30
 * @version: 1.0
 */

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    CollectionsMapper collectionsMapper;
    @Autowired
    ArticleMapper articleMapper;

    //查看自己的收藏文章
    @Override
    public List<Article> ViewCollections(int userId) {
        //用来存收藏文章
        List<Article> articleList = new ArrayList<>();

        List<Collections> collections = collectionsMapper.getCollectionsByUserId(userId);
        for (Collections collection : collections) {
            //通过收藏夹的文章id找到具体的文章
            Article article = articleMapper.selectById(collection.getArticleId());
            //加入List
            articleList.add(article);
        }

        return articleList;
    }

    //删除指定用户收藏夹
    @Override
    public Integer deleteCollections(int userId) {
        return collectionsMapper.deleteCollectionsByUserId(userId);
    }
}
